package com.lin;

import java.util.Arrays;
import java.lang.String;

public class Solution02 {
    public String sortString(String s) {
        int[] nums = new int[26];
        for (int i = 0;i<s.length();i++) {
            nums[s.charAt(i)-'a']++;
        }
        StringBuffer result = new StringBuffer();
        while (result.length()<s.length()){
            for(int i=0;i<26;i++){
                if(nums[i]>0){
                    result.append((char)(i+'a'));
                    nums[i]--;
                }
            }
            for(int i=25;i>0;i--){
                if(nums[s.charAt(i)]>0){
                    result.append((char)(i+'a'));
                    nums[i]--;
                }
            }
        }
        return result.toString();
    }
}
